package com.tevintao.marketingsystem.provider;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.task.ReportIcon;
import com.tevintao.marketingsystem.task.ReportMessage;

import java.util.ArrayList;
import java.util.List;

public class ChatFriendBean {

    private String username;
    private String account;
    private boolean updated;
    private String coverPath;
    private boolean updatedCover;
    private String nickname;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public boolean isUpdated() {
        return updated;
    }

    public void setUpdated(boolean updated) {
        this.updated = updated;
    }

    public String getCoverPath() {
        return coverPath;
    }

    public void setCoverPath(String coverPath) {
        this.coverPath = coverPath;
    }

    public boolean isUpdatedCover() {
        return updatedCover;
    }

    public void setUpdatedCover(boolean updatedCover) {
        this.updatedCover = updatedCover;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public static List<ChatFriendBean> getNoReoprtFriend(Context context) {

        List<ChatFriendBean> beanList = new ArrayList<>();
        Uri uri = Uri.parse("content://report_friend_icon");
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        LogManager.getInstance().logError("getNoReoprtFriend cursor count = " + cursor.getCount());
        ChatFriendBean bean = null;
        while (cursor != null && cursor.moveToNext()) {
            bean = new ChatFriendBean();
            bean.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            bean.setAccount(cursor.getString(cursor.getColumnIndex("account")));
            bean.setUpdated(cursor.getInt(cursor.getColumnIndex("updated")) > 0);
            bean.setCoverPath(cursor.getString(cursor.getColumnIndex("coverPath")));
            bean.setUpdatedCover(cursor.getInt(cursor.getColumnIndex("updatedCover")) > 0);
            bean.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            beanList.add(bean);
        }
        if (null != cursor) {
            cursor.close();
        }
        return beanList;
    }


    public static List<ChatFriendBean> getNoUpdateFriend() {

        List<ChatFriendBean> beanList = new ArrayList<>();
        Cursor cursor = ChatNewChatDB.getInstance().getNoUpdateFriendIcon();

        LogManager.getInstance().logError("getNoUpdateFriend cursor count = " + cursor.getCount());
        ChatFriendBean bean = null;
        while (cursor != null && cursor.moveToNext()) {
            bean = new ChatFriendBean();
            bean.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            bean.setAccount(cursor.getString(cursor.getColumnIndex("account")));
            bean.setUpdated(cursor.getInt(cursor.getColumnIndex("updated")) > 0);
            bean.setCoverPath(cursor.getString(cursor.getColumnIndex("coverPath")));
            bean.setUpdatedCover(cursor.getInt(cursor.getColumnIndex("updatedCover")) > 0);
            bean.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            beanList.add(bean);
        }
        if (null != cursor) {
            cursor.close();
        }
        return beanList;
    }


    public static ChatFriendBean getOneNoUpdateFriend() {
        Cursor cursor = ChatNewChatDB.getInstance().getNoUpdateFriendIcon();

        LogManager.getInstance().logError("getNoUpdateFriend cursor count = " + cursor.getCount());
        ChatFriendBean bean = null;
        if (cursor != null && cursor.moveToNext()) {
            bean = new ChatFriendBean();
            bean.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            bean.setAccount(cursor.getString(cursor.getColumnIndex("account")));
            bean.setUpdated(cursor.getInt(cursor.getColumnIndex("updated")) > 0);
            bean.setCoverPath(cursor.getString(cursor.getColumnIndex("coverPath")));
            bean.setUpdatedCover(cursor.getInt(cursor.getColumnIndex("updatedCover")) > 0);
            bean.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
        }
        if (null != cursor) {
            cursor.close();
        }
        return bean;
    }

    public static boolean updateIconPath(Context context, String wxId) {
        Uri uri = Uri.parse("content://report_friend_icon");
        ContentValues contentValues = new ContentValues();
        contentValues.put("coverPath", ReportIcon.iconPath + "/" + wxId + ".jpg");
        return context.getContentResolver().update(uri, contentValues, "username=?", new String[]{wxId}) > 0;
    }

    public static boolean deleteChatMessage(Context context, int id) {
        Uri uri = Uri.parse("content://report_friend_icon");
        return context.getContentResolver().delete(uri, "id=?", new String[]{"" + id}) > 0;
    }


    @Override
    public String toString() {
        return " username = " + username + " account = " + account + " updated = " + updated + " coverPath = " + coverPath + " updatedCover = " + updatedCover + " nickname = " + nickname;
    }
}
